using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pheonix.CodeLibrary;
using Pheonix.Fashionzoomer.BLL;
using Pheonix.CodeLibrary.Web;

public partial class control_RegistrationBox : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    { 
        // Initializing the Regular expression for regular expression validators to support dynamic expression change
        ((RegularExpressionValidator)signupBox.FindControl("emailValidate")).ValidationExpression = RegExUtil.EmailFormat;
        ((RegularExpressionValidator)signupBox.FindControl("passwordValidate")).ValidationExpression = RegExUtil.PasswordFormat;
        
        // Initializing the Image Button image URL to support dynamic stylesheet change
        ((ImageButton)signupBox.FindControl("register")).ImageUrl = "~/App_Themes/" + Page.StyleSheetTheme + "/image/signup.jpg";
    
        // Initializing the javascript for username textbox click event
        ((TextBox)signupBox.FindControl("userName")).Attributes.Add("onclick", "javascript:this.value='';javascript:this.style.color='#cc0000';");
    }

    protected void validateRegistration_Validate(object sender, ServerValidateEventArgs e)
    {
        CustomValidator registrationValidator = null;
        try
        {
            // Getting the reference of the validation summary control
            registrationValidator = (CustomValidator)signupBox.FindControl("validateRegistration");

            // if the user do not agree terms, discontinue
            if (((CheckBox)signupBox.FindControl("agreedTerms")).Checked == false)
            {
                e.IsValid = false;
                registrationValidator.ErrorMessage = "You must agree terms to signup";
                return;
            }

            // Check whether the username is available
            string userName = Utility.FilterInputText(((TextBox)signupBox.FindControl("userName")).Text);
            e.IsValid = UserManager.IsUserNameAvailable(userName);
            registrationValidator.ErrorMessage = "This email is already registered";
        }
        catch (Exception ex)
        {
            // If any exception or server error occured
            e.IsValid = false;

            // If the object is found
            if (registrationValidator != null)
            {
                registrationValidator.ErrorMessage = "There was a problem, try again.";
            }
        }
    }

    protected void register_Click(object sender, EventArgs e)
    {
        try
        {
            // Filtering the input text for username and password
            string userName = Utility.FilterInputText(((TextBox)signupBox.FindControl("userName")).Text);
            string password = Utility.FilterInputText(((TextBox)signupBox.FindControl("password")).Text);

            // Getting terms agreement value
            bool agreedTerms = ((CheckBox)signupBox.FindControl("agreedTerms")).Checked;

            // If the page is valid and terms are agreed
            if (Page.IsValid)
            {
                // Register the user, and the user gets logged in for instant access
                SessionManager.LoggedUser = UserManager.RegisterNewUser(userName, userName, password,
                                            (int)CommonRoles.BasicUser);
            }
        }
        catch (Exception ex)
        {
            // If any server error occurs, go to error page
            Response.Redirect("~/public/error.aspx");
        }

        // If user successful logged in
        if (SessionManager.IsUserLoggedIn)
        {
            // Redirect to confirmation page
            Page.Response.Redirect("~/public/confirmation.aspx");
        }
    }
}
